package com.atguigu.guli.service.controller.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.guli.service.emty.VodProperties;
import com.atguigu.guli.service.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.ibatis.reflection.ExceptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
@Slf4j
public class MediaServiceImpl implements MediaService {
    @Autowired
    VodProperties vodProperties;

    @Override
    public String upload(MultipartFile file) {
        try {
            //获取文件名
            String filename = file.getOriginalFilename();
            //获取文件的流
            InputStream is = file.getInputStream();
            //创建请求对象
            UploadStreamRequest request = new UploadStreamRequest(vodProperties.getAccessKeyId(), vodProperties.getAccessKeySecret(),
                    filename, filename, is);
            //设置工作流id,
            request.setWorkflowId(vodProperties.getSetWorkflowId());
            //上传 客户端对象
            UploadVideoImpl upload = new UploadVideoImpl();
            //执行上传请求
            UploadStreamResponse response = upload.uploadStream(request);
            if (response.isSuccess()) {
                /*System.out.print("VideoId=" + response.getVideoId() + "\n");*/
                return response.getVideoId();
            } else {
                /* 如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因 */
                System.out.print("VideoId=" + response.getVideoId() + "\n");
                System.out.print("ErrorCode=" + response.getCode() + "\n");
                System.out.print("ErrorMessage=" + response.getMessage() + "\n");
                throw new RuntimeException("上传视屏失败");
            }
        } catch (IOException e) {
            log.error(ExceptionUtils.getStackTrace(e));
            throw new RuntimeException("上传视屏失败");
        }

    }

    @Override
    public String getUrl(String vid) {
        try {
            String regionId = vodProperties.getRegionId();  // 点播服务接入区域
            DefaultProfile profile = DefaultProfile.getProfile(regionId, vodProperties.getAccessKeyId(), vodProperties.getAccessKeySecret());
            DefaultAcsClient client = new DefaultAcsClient(profile);
            GetPlayInfoResponse response = new GetPlayInfoResponse();

            // response = getPlayInfo(client);
            GetPlayInfoRequest request = new GetPlayInfoRequest();
            request.setVideoId(vid);
            response = client.getAcsResponse(request);

            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            //播放地址
            String url = null;
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                /*System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");*/
                url = playInfo.getPlayURL();
            }
            return url;
        } catch (Exception e) {
            /*   System.out.print("ErrorMessage = " + e.getLocalizedMessage());*/
            log.error(ExceptionUtils.getStackTrace(e));
            throw new RuntimeException("获取地址失败");
        }


    }

    @Override
    public String getAuth(String vid) {
        try {
            String regionId = vodProperties.getRegionId();  // 点播服务接入区域
            DefaultProfile profile = DefaultProfile.getProfile(regionId, vodProperties.getAccessKeyId(), vodProperties.getAccessKeySecret());
            DefaultAcsClient client = new DefaultAcsClient(profile);

            GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();

            // response = getVideoPlayAuth(client);
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(vid);
            response = client.getAcsResponse(request);
            //播放凭证
            System.out.print("PlayAuth = " + response.getPlayAuth() + "\n");
            //VideoMeta信息
            System.out.print("VideoMeta.Title = " + response.getVideoMeta().getTitle() + "\n");
            return response.getPlayAuth();
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
            log.error(ExceptionUtils.getStackTrace(e));
            throw new RuntimeException("获取凭证失败");
        }
    }
}





